<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Create a Directory Map
 *
 * Reads the specified directory and builds an array
 * representation of it.  Sub-folders contained with the
 * directory will be mapped as well.
 *
 * @access  public
 * @param string  path to source
 * @param int   depth of directories to traverse (0 = fully recursive, 1 = current dir, etc)
 * @return  array
 */
if ( ! function_exists('directory_map'))
{
    function directory_map($source_dir, $directory_depth = 0, $hidden = FALSE)
    {
        if ($fp = @opendir($source_dir))
        {
            $filedata = array();
            $new_depth  = $directory_depth - 1;
            $source_dir = rtrim($source_dir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;

            while (FALSE !== ($file = readdir($fp)))
            {
                // Remove '.', '..', and hidden files [optional]
                if ( ! trim($file, '.') OR ($hidden == FALSE && $file[0] == '.'))
                {
                    continue;
                }

                if (($directory_depth < 1 OR $new_depth > 0) && @is_dir($source_dir.$file))
                {
                    $filedata[$file] = directory_map($source_dir.$file.DIRECTORY_SEPARATOR, $new_depth, $hidden);
                }
                else
                {
                    $filedata[] = $file;
                }
            }

            closedir($fp);
            return $filedata;
        }

        return FALSE;
    }
}



/**
 * Create a Directory Make
 *
 * Reads the specified directory and builds an array
 * representation of it.  Sub-folders contained with the
 * directory will be mapped as well.
 *
 * @access	public
 * @param	string	path to source
 * @param	int		depth of directories to traverse
 * @return	bool
 */
if ( ! function_exists('directory_make'))
{
    function directory_make($path, $permission = 0755)
    {
        if (!file_exists($path)) {
            mkdir($path, $permission, TRUE);
        }

        if (file_exists($path)) {
            return true;
        } else {
            return false;
        }
    }
}

if ( ! function_exists('dircopy'))
{
    function dircopy($src, $dest, $folder_permission = 0755, $file_permission = 0644) {
        $res = false;
         
        $src = str_replace('\\', '/', $src);
        $src = str_replace('//', '/', $src);
        $dest = str_replace('\\', '/', $dest);
        $dest = str_replace('//', '/', $dest);

        //file copy
        if ( is_file($src) ) {
            if(is_dir($dest)) {
                if ($dest[ strlen($dest)-1 ] != '/') {
                    $__dest = $dest . "/";
                }

                $__dest .= basename($src);
            } else {
                $__dest = $dest;
            }

            $res = copy($src, $__dest);

            chmod($__dest, $file_permission);
        }
        //directory copy
        elseif ( is_dir($src) ) {
            if ( !is_dir($dest) ) {
                directory_make($dest, $folder_permission);
                chmod($dest, $folder_permission);
            }

            if ( $src[strlen($src)-1] != '/') {
                $__src = $src . '/';
            } else {
                $__src = $src;
            }

            if ($dest[strlen($dest) - 1]!='/') {
                $__dest = $dest . '/';
            } else {
                $__dest = $dest;
            }

            $res = true;
            $handle = opendir($src);
            while ( $file = readdir($handle) ) {
                if($file != '.' && $file != '..') {
                    $res = dircopy($__src . $file, $__dest . $file, $folder_permission, $file_permission);
                }
            }

            closedir($handle);
        } else {
            $res = false;
        }

        return $res;
    }
}


if ( ! function_exists('dir_rename'))
{
    function dir_rename($src, $dst) {
        if (directory_map($src)) {
            return FALSE;
        } else {
            dircopy($src, $dst);
            return dir_delete($src);
        }        
    }
   
}
/**
 * 删除空目录的,不空会失败
 */
if ( ! function_exists('dir_delete'))
{
    function dir_delete($dir) {
        if (directory_map($dir) != FALSE) {
            return FALSE;
        } else { 
            return @rmdir($dir);
        }        
    }  
}
/**
 * 非空目录删除，现为一级（目录下为文件的）
 */
if ( ! function_exists('dir_delete_full'))
{
    function dir_delete_full($dir) {
        $arr = directory_map($dir);  
        if ($arr == FALSE) {
            return TRUE;
        }
        foreach ($arr as $file) {
            @unlink($dir . '/' . $file);
        }
        return dir_delete($dir);
    }  
}



if ( ! function_exists('dir_name_check'))
{
    function dir_name_check($dir_name) {  
        if(preg_match("/^[a-z0-9_]{1,20}$/", $dir_name)) {
			return true;   
		}
        return false;   
    }
   
}
/* End of file directory_helper.php */
/* Location: ./system/helpers/directory_helper.php */